﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace MyButton
{
    [DefaultEvent("Click"), ToolboxBitmap(typeof(MyButton), "Res.MyICO.ico")]
    public partial class MyButton : Button
    {
        private State _buttonState = State.Normal;

        //从资源文件载入图片
        private Image _normalImg = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton.Res.btn_normal.png"));
        private Image _highLightImg = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton.Res.btn_highlight.png"));
        private Image _downImg = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton.Res.btn_down.png"));
        private Image _focusedImg = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton.Res.btn_focus.png"));
        private Image _disabledImg = Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream(@"MyButton.Res.btn_disabled.png"));

        #region  Properties
        [Category("自定义属性"), Description("控件的正常背景图片")]
        public Image NormalImg
        {
            get
            {
                return _normalImg;
            }
            set
            {
                _normalImg = value;
                BackgroundImage = value;
            }

        }

        [Category("自定义属性"), Description("当鼠标进入控件的可见部分是的背景图片")]
        public Image HighLightImg
        {
            get
            {
                return _highLightImg;
            }
            set
            {
                _highLightImg = value;
            }
        }

        [Category("自定义属性"), Description("当控件被按下时的背景图片")]
        public Image DownImg
        {
            get
            {
                return _downImg;
            }
            set
            {
                _downImg = value;
            }
        }

        [Category("自定义属性"), Description("当控件拥有焦点时的背景图片")]
        public Image FocusedImg
        {
            get
            {
                return _focusedImg;
            }
            set
            {
                _focusedImg = value;
            }
        }

        [Category("自定义属性"), Description("控件禁止时的背景图片")]
        public Image DisabledImg
        {
            get
            {
                return _disabledImg;
            }
            set
            {
                _disabledImg = value;
            }
        }

        #endregion  Properties

        //===============================================================================================

        // Constructor
        public MyButton()
            : base()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        #region  OverrideMethods
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            _buttonState = State.Highlight;
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (_buttonState == State.Highlight && Focused)
            {
                _buttonState = State.Focused;

            }
            else if (_buttonState == State.Focused)
            {
                _buttonState = State.Focused;
            }
            else
            {
                _buttonState = State.Normal;
            }
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs mevent)
        {

            if (mevent.Button == MouseButtons.Left && mevent.Clicks == 1)
            {
                _buttonState = State.Down;
                base.OnMouseDown(mevent);
            }
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            if (mevent.Button == MouseButtons.Left && mevent.Clicks == 1)
            {
                if (ClientRectangle.Contains(new Point(mevent.X, mevent.Y)))
                {
                    _buttonState = State.Highlight;
                }
                else
                {
                    _buttonState = State.Focused;
                }
                base.OnMouseUp(mevent);
            }
        }

        // The event of OnLeave and OnEnter to make object lost focus or get focus 
        protected override void OnLeave(EventArgs e)
        {
            _buttonState = State.Normal;
            base.OnLeave(e);
        }

        protected override void OnEnter(EventArgs e)
        {
            _buttonState = State.Focused;
            base.OnEnter(e);
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            base.OnPaintBackground(pevent);

            Graphics g = pevent.Graphics;

            if (Enabled == false)
            {
                _buttonState = State.Disabled;
            }

            switch (_buttonState)
            {
                case State.Highlight:
                    g.DrawImage(_highLightImg, new Rectangle(0, 0, Width, Height));
                    break;
                case State.Down:
                    g.DrawImage(_downImg, new Rectangle(0, 0, Width, Height));
                    break;
                case State.Focused:
                    g.DrawImage(_focusedImg, new Rectangle(0, 0, Width, Height));
                    break;
                case State.Disabled:
                    g.DrawImage(_disabledImg, new Rectangle(0, 0, Width, Height));
                    break;
                default:
                    g.DrawImage(_normalImg, new Rectangle(0, 0, Width, Height));
                    break;
            }
            Console.WriteLine(_buttonState);
            DrawText(g);
        }

        protected override void Dispose(bool disposing)
        {
            if (_normalImg != null || _highLightImg != null || _downImg != null || _focusedImg != null || _disabledImg != null)
            {
                _normalImg.Dispose();
                _highLightImg.Dispose();
                _downImg.Dispose();
                _focusedImg.Dispose();
                _disabledImg.Dispose();
            }
            base.Dispose(disposing);
        }
        #endregion  OverrideMethods

        #region  PrivateMethods
        private void DrawText(Graphics g)
        {
            int x = 0;
            int y = 0;
            Size s = g.MeasureString(Text, Font).ToSize();
            x = (Width - s.Width) / 2;
            y = (Height - s.Height) / 2;
            g.DrawString(Text, Font, Brushes.Black, new Point(x, y));
        }
        #endregion  PrivateMethods

        #region Enum
        internal enum State
        {
            Normal,  //正常

            Highlight,  // 鼠标进入

            Down,  //鼠标按下

            Focused,  // 获得焦点

            Disabled  //控件禁止
        }
        #endregion  Enum
    }
}
